package com.yu.waterstation.app.moudles.student.controller;

import com.yu.waterstation.common.common.util.ip.IpUtil;
import com.yu.waterstation.common.entity.app.Address;
import com.yu.waterstation.common.entity.app.dto.StudentDTO;
import com.yu.waterstation.common.entity.app.form.*;
import com.yu.waterstation.app.moudles.student.service.StudentServiceImpl;

import com.yu.waterstation.common.common.annotation.NeedToken;
import com.yu.waterstation.common.common.annotation.VisitorAccess;
import com.yu.waterstation.common.common.enums.TokenType;
import com.yu.waterstation.common.common.util.result.Result;
import com.yu.waterstation.app.common.util.session.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "学生服务")
@RestController
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentServiceImpl studentService;

    @ApiOperation("游客登录")
    @VisitorAccess
    @PostMapping("/login/guest")
    public Result<StudentDTO> guestLogin() {
        StudentDTO studentInfo = studentService.getStudentInfo(0);
        if (studentInfo == null)
            return Result.fail("游客账号不存在");
        if (!studentInfo.getStatus())
            return Result.fail("游客账号已被冻结");
        SessionUtil.setStudentSession(studentInfo); // 缓存会话信息
        return Result.ok(studentInfo);
    }

    @ApiOperation("通过学号密码登录或注册")
    @PostMapping("/login")
    public Result<StudentDTO> loginByStudentNoAndPassword(@RequestBody StudentLoginForm loginForm, HttpServletRequest request) {
        return studentService.loginByStudentNoAndPassword(loginForm, IpUtil.getIp(request));
    }


    @ApiOperation("通过微信登录")
    @PostMapping("/login/wx")
    public Result<StudentDTO> loginByWx(@RequestBody WeixinLoginForm weixinLoginForm) {
        return studentService.loginByWx(weixinLoginForm);
    }

    @ApiOperation("退出登录")
    @VisitorAccess
    @NeedToken(TokenType.ENUM_student_token)
    @DeleteMapping("/logout")
    public Result logout(HttpServletRequest request) {
        return Result.ok(SessionUtil.studentLogout(request));
    }

    @ApiOperation("获取学生数据")
    @VisitorAccess
    @NeedToken(TokenType.ENUM_student_token)
    @GetMapping("/info")
    public Result<StudentDTO> getStudentInfo(HttpServletRequest request) {
        System.out.println("studentdId = " + SessionUtil.getStudentIdFromRequest(request));
        StudentDTO studentInfo = studentService.getStudentInfo(SessionUtil.getStudentIdFromRequest(request));
        SessionUtil.setStudentSession(studentInfo); // 延长token的过期时间
        return Result.ok(studentInfo);
    }

    @ApiOperation("绑定微信")
    @NeedToken(TokenType.ENUM_student_token)
    @PutMapping("/weixin")
    public Result<String> bindWeiXin(@RequestBody WeixinLoginForm weixinLoginForm, HttpServletRequest request) throws Exception {
        return Result.ok(studentService.bindWeiXin(SessionUtil.getStudentIdFromRequest(request), weixinLoginForm));
    }

    @ApiOperation("更新密码")
    @NeedToken(TokenType.ENUM_student_token)
    @PutMapping("/password")
    public Result<Integer> updateStudent(@ApiParam("修改密码表单") @RequestBody UpdatePasswordForm vo, HttpServletRequest request) throws Exception {
        return Result.ok(studentService.updatePassword(SessionUtil.getStudentIdFromRequest(request), vo));
    }

    @ApiOperation("微信授权修改密码")
    @NeedToken(TokenType.ENUM_student_token)
    @PutMapping("/password/weixin")
    public Result updatePasswordByWeiXin(@ApiParam("参数表单") @RequestBody UpdatePasswordByWeiXinForm vo, HttpServletRequest request) throws Exception {
        return Result.ok(studentService.updatePasswordByWeiXin(SessionUtil.getStudentIdFromRequest(request), vo));
    }

    @ApiOperation("更新信息")
    @NeedToken(TokenType.ENUM_student_token)
    @PutMapping
    public Result updateStudent(@ApiParam("更新学生信息表单") @RequestBody UpdateStudentForm updateStudentForm, HttpServletRequest request) throws Exception {
        return Result.ok(studentService.updateStudent(SessionUtil.getStudentIdFromRequest(request), updateStudentForm));
    }


    @ApiOperation("设置寝室")
    @NeedToken(TokenType.ENUM_student_token)
    @PutMapping("/address/{addressId}")
    public Result<Address> setAddress(@PathVariable Integer addressId, HttpServletRequest request) throws Exception {
        return Result.ok(studentService.setAddress(SessionUtil.getStudentIdFromRequest(request), addressId));
    }


}
